package com.example.demo.common.result;
//import io.swagger.annotations.ApiModelProperty;
//import lombok.Data;

import com.github.pagehelper.PageInfo;
import com.example.demo.common.result.ResultCode;
import com.example.demo.common.result.ResultMessage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author NieChangan
 * @Description 公共结果返回
 */
//@Data
public class Result {
//    @ApiModelProperty(value = "是否成功")
    private Boolean success;

//    @ApiModelProperty(value = "返回码")
    private Integer code;

//    @ApiModelProperty(value = "返回消息")
    private String message;

//    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data = new HashMap<>();

    private Object singleData;

    private URL url;


    public Object getSingleData() {
        return singleData;
    }

    public void setSingleData(Object singleData) {
        this.singleData = singleData;
    }

    public void setUrl(URL url) {
        this.url = url;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    /**
     * 构造方法私有化,里面的方法都是静态方法
     * 达到保护属性的作用
     */
    private Result(){

    }

    /**
     * 这里是使用链式编程
     */
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static Result ok(String msg){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(msg);
        return result;
    }

    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.COMMON_FAIL.getCode());
        result.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return result;
    }

    public static Result error(String message){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(999);
        result.setMessage(message);
        return result;
    }
    public static Result error(ResultCode resultCode){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }


    /**
     * 自定义返回成功与否
     * @param success
     * @return
     */
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result message(ResultMessage resultMessage){
        this.setMessage(resultMessage.getMessage());
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(String key,Object value){

        data.put(key,value);
        this.setData(data);
        return this;
    }

    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }


    public static Result okData(String key,Object value){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.data.put(key,value);
        return result;
    }

    public static Result okSingle(Object value){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setSingleData(value);
        return result;
    }

    public static Result okUrl(URL value){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setUrl(value);
        return result;
    }


    /**
     *
     * @param i CRUD 返回的数字
     * @param successMessage 成功文字提示
     * @param errorMessage  失败文字提示
     * @return
     */
    public static Result ok(Integer i,ResultMessage successMessage,ResultMessage errorMessage){
        return i > 0 ? Result.ok().message(successMessage) : Result.error().message(errorMessage);
    }

    /**
     *
     * @param i CRUD 返回的数字
     * @return
     */
    public static Result okDeleteBatch(Integer i){
        return i > 0 ? Result.ok().message("成功删除"+ i + "条数据！") : Result.error().message(ResultMessage.DELETE_ERR);
    }

    public static Result okInsertBatch(Integer i){
        return i > 0 ? Result.ok().message("成功添加"+ i + "条数据！") : Result.error().message(ResultMessage.ADD_ERR);
    }

    public static Result okInsertBatch(Integer i,String errMessage){
        return i > 0 ? Result.ok().message("成功添加"+ i + "条数据！") : Result.error().message(errMessage);
    }

    public static Result okUpdateBatch(Integer i,String errMessage){
        return i > 0 ? Result.ok().message("成功更新"+ i + "条数据！") : Result.error().message(errMessage);
    }

    /**
     *
     * @param i CRUD 返回的数字
     * @return
     */
    public static Result ok(boolean b){
        return b ? Result.ok().message("操作成功") : Result.error().message("操作失败");
    }


    /**
     * 统一PageInfo的信息
     * 返回 以list 和 total 为 key 的值
     * @param pageInfo
     * @return
     */
    public static Result okPageInfo(PageInfo pageInfo){
        return Result.ok().data("list",pageInfo.getList()).data("total",pageInfo.getTotal());
    }
}